package com.st.reqandresp;


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Date;

/**
 * Created by zhangshuai on 2017/8/27.
 * 自己封装相应信息
 */
public class Response {

//    Content-Language:zh-CN
//    Content-Type:text/html;charset=utf-8
//    Date:Sun, 27 Aug 2017 14:02:36 GMT
//    Server:Apache-Coyote/1.1
//    Transfer-Encoding:chunked
    private static final String CRLF = "\r\n";
    private static final String BLANK = " ";

    private String characterEncoding;

    private String contentType;

    private StringBuilder responseHeadInfo;
    private StringBuilder responseContent;

    private BufferedWriter bufferedWriter;

    public Response() {
        responseHeadInfo = new StringBuilder();
        responseContent = new StringBuilder();
        characterEncoding = "UTF-8";
        contentType = "text/html;charset=UTF-8";
    }

    public Response(OutputStream outputStream){
        this();
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
    }

    /**
     * 此处不应该throws异常,因为异常也需要有header部分
     * @param client
     * @throws IOException
     */
    public Response(Socket client){
        this();
        try{
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        }catch (Exception e){
            responseContent.append("");
        }
    }

    public String getCharacterEncoding() {
        return characterEncoding;
    }

    public void setCharacterEncoding(String characterEncoding) {
        this.characterEncoding = characterEncoding;
        this.setContentType("text/html;charset=" + characterEncoding);
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    private void generateRespHeader(int code){
        responseHeadInfo.append("HTTP/1.0").append(BLANK).append(code)
                .append(BLANK).append("OK").append(CRLF).append("Server:mySimpleTomcat/1.0.0")
                .append(CRLF).append("Date:").append(new Date()).append(CRLF)
                .append("Content-Type:").append(contentType).append(CRLF).append("Content-Length:")
                .append(responseContent.toString().getBytes().length).append(CRLF).append(CRLF);
    }

    public Response print(Object obj){
        responseContent.append(obj);
        return this;
    }

    public Response println(Object obj){
        responseContent.append(obj).append(CRLF);
        return this;
    }

    public void sendToClient() throws IOException{
        generateRespHeader(200);
        bufferedWriter.append(responseHeadInfo.toString());

        bufferedWriter.append(responseContent.toString());
        bufferedWriter.flush();
        this.close();
    }

    public void close(){
        if(bufferedWriter != null){
            try{
                bufferedWriter.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

}
